package anotador;

import gate.*;
import gate.creole.ResourceInstantiationException;
import gate.util.web.WebCrimeReportAnalyser.SortedAnnotationList;
import java.net.*;
import java.util.*;
import java.util.ArrayList;

/**
 * @author Mauricio
 */
public class GateDocument {
    private String name;
    Document doc;


    public String getName()
    {
        return name;
    }

    public GateDocument(URL url)
    {
        name = url.getPath();
        FeatureMap params = Factory.newFeatureMap();
        params.put("sourceUrl", url);
        params.put("preserveOriginalContent", new Boolean(true));
        params.put("collectRepositioningInfo", new Boolean(true));
        try {
            doc = (Document) Factory.createResource("gate.corpora.DocumentImpl", params);
        } catch (ResourceInstantiationException ex) {
            ex.printStackTrace();
        }
    }

    public Document getDoc()
    {
        return doc;
    }

    public String toHTML(ArrayList<String> outputAnnotations)
    {
        FeatureMap features = doc.getFeatures();
        String originalContent = (String)features.get(GateConstants.ORIGINAL_DOCUMENT_CONTENT_FEATURE_NAME);

        try {
            String startTagPart_1 = "<span GateID=\"";
            String startTagPart_2 = "\" title=\"";
            String startTagPart_3 = "\" style=\"background:Yellow;\">";
            String endTag = "</span>";

            Set annotTypesRequired = new HashSet();

            Iterator oai = outputAnnotations.iterator();
            while(oai.hasNext())
            {
                String annotTypeRequired = (String)oai.next();
                annotTypesRequired.add(annotTypeRequired);
            }

            StringBuffer editableContent = new StringBuffer(originalContent);

            long insertPositionEnd;
            long insertPositionStart;
            Annotation currAnnot;
            Iterator it =  doc.getAnnotations().get(annotTypesRequired).iterator();
            SortedAnnotationList sortedAnnotations = new SortedAnnotationList();

            while(it.hasNext()) {
                currAnnot = (Annotation) it.next();
                sortedAnnotations.addSortedExclusive(currAnnot);
            }

            for(int i=sortedAnnotations.size()-1; i>=0; --i)
            {
                currAnnot = (Annotation) sortedAnnotations.get(i);
                insertPositionStart = currAnnot.getStartNode().getOffset().longValue();
                //insertPositionStart = info.getOriginalPos(insertPositionStart);
                insertPositionEnd = currAnnot.getEndNode().getOffset().longValue();
                //insertPositionEnd = info.getOriginalPos(insertPositionEnd, true);

                editableContent.insert((int) insertPositionEnd, endTag);
                editableContent.insert((int) insertPositionStart, startTagPart_3);
                String ann;
                if (currAnnot.getType().equals("Token"))
                   ann = (String)currAnnot.getFeatures().get("category");
                else if (currAnnot.getType().equals("Lookup"))
                    ann = (String)currAnnot.getFeatures().get("majorType");
                else
                    ann = currAnnot.getType();
                editableContent.insert((int) insertPositionStart, ann);
                editableContent.insert((int) insertPositionStart, startTagPart_2);
                editableContent.insert((int) insertPositionStart, currAnnot.getId().toString());
                editableContent.insert((int) insertPositionStart, startTagPart_1);
            }

            editableContent.insert(0, "Número de definiciones encontradas: " +sortedAnnotations.size()+"</br></br>");

            return editableContent.toString().replaceAll("\n", "<br/>");

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
